package controllers

import (
	"flashapi/enums"
	"flashapi/utils"
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"os"
	"strconv"
	"strings"
	"time"
)

type GitListController struct {
	BaseController
}

func (c *GitListController) Index() {
	//判断是否登录
	c.checkLogin()
	//需要权限控制
	c.checkAuthor()

	c.LayoutSections = make(map[string]string)
	c.LayoutSections["headcssjs"] = "home/login_headcssjs.html"
	c.LayoutSections["footerjs"] = "home/login_footerjs.html"
	c.setTpl("gitlist/index.html")
}

func (c *GitListController) GitShell() {
	//定义返回的数据结构
	result := make(map[string]interface{})

	g := c.GetSession("git_start")
	result["mes"] = "发布代码执行中"
	result["git_start"] = g
	if g == nil {
		result["mes"] = "发布代码执行中"
		//c.Data["git_start"] = g
		go utils.ExecShell("main.sh") //协程异步执行
		c.SetSession("git_start", 1)
	}

	//out, _ := utils.ExecShell("main.sh")

	result["data"] = "代码已发布执行ok"
	c.Data["json"] = result
	c.ServeJSON()
}

func (c *GitListController) ShowShell() {
	//需要权限控制
	//c.checkAuthor()
	//定义返回的数据结构
	result := make(map[string]interface{})

	_data := strings.Split(utils.OsIoutil("/tmp/1.log"), "\n")
	_len := len(_data)

	for i := 0; i < _len; i++ {
		if _data[i] == "exit" {
			c.DelSession("git_start")
		}
	}

	result["data"] = _data
	result["len"] = _len
	c.Data["json"] = result
	c.ServeJSON()
}

func (c *GitListController) List() {
	//c.LayoutSections = make(map[string]string)
	//c.LayoutSections["headcssjs"] = "home/login_headcssjs.html"
	//c.LayoutSections["footerjs"] = "home/login_footerjs.html"
	c.setTpl("gitlist/show.html", "shared/layout_base.html")
}

func (c *GitListController) Page404() {
	c.setTpl()
}
func (c *GitListController) Error() {
	c.Data["error"] = c.GetString(":error")
	c.setTpl("home/error.html", "shared/layout_pullbox.html")
}

func (c *GitListController) Test() {
	file := "./Book1.xlsx"
	os.Remove(file)
	fmt.Println(file)
	f := excelize.NewFile()
	start1 := time.Now().Unix()
	// Create a new sheet.
	//index := f.NewSheet("Sheet2")
	// Set value of a cell.
	//f.SetCellValue("Sheet2", "A2", "Hello world.")
	//f.SetCellValue("Sheet1", "B2", 100)
	for i := 0; i < 200000; i++ {
		si := strconv.Itoa(i)
		f.SetCellValue("Sheet1", "A"+si, 100)
		f.SetCellValue("Sheet1", "B"+si, 100)
		f.SetCellValue("Sheet1", "C"+si, 100)
		f.SetCellValue("Sheet1", "D"+si, 100)
		f.SetCellValue("Sheet1", "E"+si, 100)
		f.SetCellValue("Sheet1", "F"+si, 100)
		f.SetCellValue("Sheet1", "G"+si, 100)
		f.SetCellValue("Sheet1", "H"+si, 100)
		f.SetCellValue("Sheet1", "I"+si, 100)
		f.SetCellValue("Sheet1", "G"+si, 100)
		f.SetCellValue("Sheet1", "J"+si, 100)
		f.SetCellValue("Sheet1", "K"+si, 100)
	}
	// Set active sheet of the workbook.
	//f.SetActiveSheet(index)
	// Save xlsx file by the given path.
	err := f.SaveAs("./debug/Book1.xlsx")
	start2 := time.Now().Unix()
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(start2 - start1)

	c.jsonResult(enums.JRCodeSucc, "登录成功", "")
}
